package org.sample.springbootasync.config;

import java.util.concurrent.Executor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

public class ExecutorConfig extends AsyncConfigurerSupport {

	@Value("${threadpool.corepoolsize:0}")
	private int corePoolSize;

	@Value("${threadpool.maxpoolsize:0}")
	private int maxPoolSize;

	@Value("${threadpool.queuecapacity:0}")
	private int queueCapacity;

	@Override
	public Executor getAsyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		if (corePoolSize != 0) {
			executor.setCorePoolSize(corePoolSize);
		}
		if (maxPoolSize != 0) {
			executor.setMaxPoolSize(maxPoolSize);
		}
		if (queueCapacity != 0) {
			executor.setQueueCapacity(queueCapacity);
		}
		executor.initialize();
		return executor;
	}
}
